const jwt = require('jsonwebtoken')
const messageInfo = require('./messageInfo.js')
module.exports = async (req, res, next) => {
    if (req.url.startsWith('/api/account') || req.url.startsWith('/api/utils') || req.url.startsWith('/public')) {
        next()
    } else if (!req.url.startsWith('/api/account') && !req.get('Authorization')) {
        return res.status(400).json(messageInfo({ url: '/api/account' }, 301, '还未登录'))
    } else {
        try {
            let token = await jwt.verify(
                req.get('Authorization'),
                'huohua29'
            )
            if (token.user_id) await next()
            else return res.status(401).json(messageInfo(null, 401, '无效token，请重新登陆'))
        } catch (error) {
            return res.status(401).json(messageInfo(error, 401, '无效token，请重新登陆'))
        }
    }
}